package com.qd.common.sys.domain.sendmsg;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Data
@Accessors(chain = true)
@Slf4j
public class FusionMsg {
    public static final int SEND_FLAG_SMS = 0x1;
    public static final int SEND_FLAG_WEIXIN = 0x2;
    public static final int SEND_FLAG_APP = 0x4;
    private static final long serialVersionUID = 1L;
    /**
     * 优先级 0 ~ 5
     */
    private int level;

    /**
     * 发送模式
     */
    private int sendFlag;

    /**
     * 来源标识
     */
    private String fromMsgId;

    /**
     * 来源子标识
     */
    private String fromMsgSubId;

    /**
     * 发送组织
     */
    private String sendOrgId;
    /**
     * 发送模板消息，指定要发送的公众号
     */
    private String appId;

    /**
     * 接收对象
     */
    private List<MsgReceiver> msgReceiver;

    /**
     * 发送内容
     */
    private MsgContent msgContent;

    /**
     * 发送属性
     */
    private MsgProperties msgProperties;

    /**
     * url链接自动加入ui、ut、ur参数
     */
    private boolean autoMarkUser;

    public FusionMsg() {
        level = 3;
        msgReceiver = new ArrayList<>();
        msgContent = new MsgContent();
        msgProperties = new MsgProperties();
    }

    public void free() {
        if (msgReceiver != null) {
            for (MsgReceiver rev : msgReceiver) {
                rev.free();
            }
            msgReceiver.clear();
            msgReceiver = null;
        }
        if (msgContent != null) {
            msgContent.free();
            msgContent = null;
        }
        if (msgProperties != null) {
            msgProperties.free();
            msgProperties = null;
        }
        sendOrgId = null;
        fromMsgId = null;
        fromMsgSubId = null;
    }

    public boolean canSendSms() {
        return (sendFlag & SEND_FLAG_SMS) != 0x0;
    }

    public boolean canSendWeixin() {
        return (sendFlag & SEND_FLAG_WEIXIN) != 0x0;
    }

    public boolean canSendApp() {
        return (sendFlag & SEND_FLAG_APP) != 0x0;
    }


    public boolean isAutoMarkUser() {
        return autoMarkUser;
    }


    public void read(JSONObject content) {
        level = content.getIntValue("level");
        sendFlag = content.getIntValue("sendFlag");
        fromMsgId = content.getString("fromMsgId");
        fromMsgSubId = content.getString("fromMsgSubId");
        sendOrgId = content.getString("sendOrgId");

        buildReadMsgReceiver(content);
        buildReadMsgContent(content);

        buildReadMsgProperties(content);

        autoMarkUser = content.getBooleanValue("autoMarkUser");
    }

    private void buildReadMsgProperties(JSONObject content) {
        JSONObject jsonObject = content.getJSONObject("msgProperties");
        if (jsonObject != null) {
            msgProperties.read(jsonObject);
        }
    }

    private void buildReadMsgContent(JSONObject content) {
        JSONObject jsonObject = content.getJSONObject("msgContent");
        if (jsonObject != null) {
            msgContent.read(jsonObject);
        }
    }

    private void buildReadMsgReceiver(JSONObject content) {
        JSONArray jsonArray = content.getJSONArray("msgReceiver");
        if (jsonArray != null) {
            for (Object item : jsonArray) {
                if (item instanceof JSONObject) {
                    MsgReceiver recv = new MsgReceiver();
                    recv.read((JSONObject) item);
                    msgReceiver.add(recv);
                }
            }
        }
    }

    public void write(JSONObject content) {
        content.put("level", level);
        content.put("sendFlag", sendFlag);
        content.put("fromMsgId", fromMsgId);
        content.put("fromMsgSubId", fromMsgSubId);
        content.put("sendOrgId", sendOrgId);
        buildWriteMsgReceiver(content);
        buildWriteMsgContent(content);
        buildWriteMsgProperties(content);
        content.put("autoMarkUser", autoMarkUser);
    }

    private void buildWriteMsgProperties(JSONObject content) {
        JSONObject jsonObject = new JSONObject();
        content.put("msgProperties", jsonObject);
        msgProperties.write(jsonObject);
    }

    private void buildWriteMsgContent(JSONObject content) {
        JSONObject jsonObject = new JSONObject();
        content.put("msgContent", jsonObject);
        msgContent.write(jsonObject);
    }

    private void buildWriteMsgReceiver(JSONObject content) {
        JSONArray jsonArray = new JSONArray();
        content.put("msgReceiver", jsonArray);
        for (MsgReceiver item : msgReceiver) {
            JSONObject recv = new JSONObject();
            item.write(recv);
            jsonArray.add(recv);
        }
    }
}
